/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.logical.allhit.gui.model;

import com.logical.allhit.setting.Account;
import com.logical.allhit.setting.Setting;
import com.logical.allhit.setting.SettingChangeEvent;
import com.logical.allhit.setting.SettingChangeEventListener;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Sanjeet Jha
 */
public class AccountTableModel extends AbstractTableModel implements SettingChangeEventListener {

    private ArrayList<Account> accounts;
    private boolean[] isselect;
    // These are the names of the columns represented by this TableModel
    static final String[] columnNames = new String[]{"", "Account ID",
        "Balance", "Last Update","UID"};
    // These are the types of the columns represented by this TableModel
    static final Class[] columnTypes = new Class[]{Boolean.class, String.class,
         Long.class, Date.class,Long.class};

    public AccountTableModel() {
        accounts = Setting.getInstance().getAccounts();
        isselect = new boolean[accounts.size()];
        for (int i = 0; i < accounts.size(); i++) {
            isselect[i] = false;
        }
    }

    public void selectAll(boolean isSelect) {
        for (int i = 0; i < accounts.size(); i++) {
            isselect[i] = isSelect;
        }
        fireTableDataChanged();
    }

    @Override
    public int getRowCount() {
        return accounts.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public Class getColumnClass(int column) {
        return columnTypes[column];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Account account = accounts.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return isselect[rowIndex];
            case 1:
                return account.getName();
            case 2:
                return account.getBalance();
            case 3:
                return account.getLastUpdateDate();
            case 4:
                return account.getUid();
            default:
                return new Object();
        }
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        if (columnIndex == 0) {
            return true;
        }
        return false;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        Account account = accounts.get(rowIndex);
        switch (columnIndex) {
            case 0:
                isselect[rowIndex] = (Boolean) aValue;
                break;
            case 1:
                account.setName((String) aValue);
                break;
            case 2:
                account.setBalance((Long) aValue);
                break;
            case 3:
                account.setLastUpdateDate((Date) aValue);
                break;
            case 4:
                account.setUid((Long) aValue);
                break;
        }
    }

    @Override
    public void settingChenged(SettingChangeEvent evt) {
        accounts = Setting.getInstance().getAccounts();
        selectAll(false);
        fireTableDataChanged();
    }
    
    public Account getAccountAt(int rowIndex){
        return accounts.get(rowIndex);
    }
}
